package com.hanserwei.community.controller;

import com.hanserwei.community.entity.common.ApiResponse;
import com.hanserwei.community.entity.po.Role;
import com.hanserwei.community.entity.vo.UserRoleVO;
import com.hanserwei.community.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    @GetMapping("/list")
    @Operation(summary = "查询角色列表")
    public ApiResponse<List<Role>> getAllRoles() {
        return ApiResponse.success(roleService.list());
    }

    @PostMapping("/add")
    @Operation(summary = "添加角色")
    public ApiResponse<Boolean> addRole(@RequestBody Role role) {
        System.out.println(role.getRname() + role.getRemark());
        return ApiResponse.success(roleService.save(role));
    }

    @PutMapping("/update")
    @Operation(summary = "更新角色")
    public ApiResponse<Boolean> updateRole(@RequestBody Role role) {
        return ApiResponse.success(roleService.updateById(role));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除角色")
    public ApiResponse<Boolean> deleteRole(@Parameter(description = "角色ID") Integer id) {
        roleService.deletRoleById(id);
        return ApiResponse.success(true);
    }

    @PostMapping("/addUserRole")
    @Operation(summary = "添加用户角色")
    public ApiResponse<Boolean> addUserRole(@Parameter(description = "用户ID") @RequestParam Integer userId,
                                            @Parameter(description = "角色ID") @RequestParam Integer roleId) {
        return ApiResponse.success(roleService.addUserRole(userId, roleId));
    }

    @GetMapping("/listUserRole")
    @Operation(summary = "查询所有用户及其对应角色")
    public ApiResponse<List<UserRoleVO>> listUserRole() {
        return ApiResponse.success(roleService.listUserRole());
    }

    @DeleteMapping("/deleteUserRole")
    @Operation(summary = "删除用户角色")
    public ApiResponse<Boolean> deleteUserRole(@Parameter(description = "用户ID") @RequestParam Integer userId,
                                               @Parameter(description = "角色ID") @RequestParam Integer roleId) {
        return ApiResponse.success(roleService.deleteUserRole(userId, roleId));
    }
}
